package com.koicarp.university.graduate.service.service.report.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.ReportTypeDao;
import com.koicarp.university.graduate.service.entity.report.ReportType;
import com.koicarp.university.graduate.service.service.report.IReportTypeService;
import com.koicarp.university.graduate.service.vo.mission.ReportTypeVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【报表管理：报表类型表】(ReportType)表服务实现类
 *
 * @author liutao
 * @since 2020-12-03 13:44:16
 */
@Slf4j
@Service("reportTypeService")
@RequiredArgsConstructor
public class ReportTypeServiceImpl extends ServiceImpl<ReportTypeDao,ReportType> implements IReportTypeService {

    private final ReportTypeDao reportTypeDao;

    @Override
    public UniversityResponse getReportTypeWithPage(ReportTypeVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<ReportType> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        ReportType reportType = new ReportType();
        BeanUtils.copyProperties(vo,reportType);
        if (!findOne && !isPage){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.reportTypeDao.queryAll(reportType));
            response.data(listRes);
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<ReportType> result = null;
        result = this.reportTypeDao.queryReportTypeWithPage(page, vo);
        if (findOne){
            List<ReportType> list = result.getRecords();
            if (list == null || list.size() == 0){
                log.info("为空");
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL);
            } else {
                response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
                response.data(list.get(0));
            }
        } else {
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ReportType queryById(Integer id) {
        return this.reportTypeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ReportType> queryAllByLimit(int offset, int limit) {
        return this.reportTypeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param reportType 实例对象
     * @return 实例对象
     */
    @Override
    public ReportType insert(ReportType reportType) {
        this.reportTypeDao.insert(reportType);
        return reportType;
    }

    /**
     * 修改数据
     *
     * @param reportType 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse update(ReportType reportType) {
        UniversityResponse response = new UniversityResponse();;
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        int i = this.reportTypeDao.update(reportType);
        if (i<0){
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        }
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.reportTypeDao.deleteById(id) > 0;
    }
}